import React from "react";
import { Navigate, Outlet, useRoutes } from "react-router-dom";
import { AuthComponent } from "@/components/AuthComponent"; // 路由拦截
import { Lazyload } from "@/components/Lazyload"; // 懒加载
import { DatabaseFilled, DatabaseOutlined, HomeOutlined, MedicineBoxOutlined } from "@ant-design/icons";

// 路由配置
const routes = [
    {
        // 首页
        path: "/",
        element: <AuthComponent>{Lazyload("Index")}</AuthComponent>,
        children: [
            // 主页
            {
                label: "主页",
                path: "/",
                icon: <HomeOutlined />,
                element: Lazyload("Home"),
            },
            // 数据管理
            {
                label: "数据管理",
                path: "/cmn",
                icon: <DatabaseOutlined />,
                element: <Outlet />, // 二级路由
                children: [
                    {
                        path: "/cmn",
                        isHide: true, // 是否显示在侧边栏
                        element: <Navigate to={"dict"} />, // 重定向
                    },
                    // 数据字典
                    {
                        label: "数据字典",
                        path: "/cmn/dict",
                        element: Lazyload("cmn/dict"),
                    },
                ],
            },
            {
                // 医院管理
                label: "医院管理",
                path: "/hospital",
                icon: <MedicineBoxOutlined />,
                element: <Outlet />, // 二级路由
                children: [
                    {
                        path: "/hospital",
                        isHide: true, // 是否显示在侧边栏
                        element: <Navigate to={"hospitalSet"} />, // 重定向
                    },
                    // 医院设置
                    {
                        label: "医院设置",
                        path: "/hospital/hospitalSet",
                        element: Lazyload("hospital/hospitalSet"),
                    },
                    // 医院列表
                    {
                        label: "医院列表",
                        path: "/hospital/hospitalList",
                        element: Lazyload("hospital/hospitalList"),
                    },
                    // 医院详情
                    {
                        label: "医院详情",
                        isHide: true, // 是否显示在侧边栏
                        path: "/hospital/hospitalDetail/:id",
                        element: Lazyload("hospital/hospitalDetail"),
                    },
                    // 科室管理
                    {
                        label: "科室管理",
                        isHide: true, // 是否显示在侧边栏
                        path: "/hospital/department/:hoscode",
                        element: Lazyload("hospital/department"),
                    },
                ],
            },
            {
                // 商品管理
                label: "商品管理",
                path: "/product",
                icon: <DatabaseFilled />,
                element: <Outlet />, // 二级路由
                children: [
                    {
                        path: "/product",
                        isHide: true, // 是否显示在侧边栏
                        element: <Navigate to={"trademark"} />, // 重定向
                    },
                    // 品牌管理
                    {
                        label: "品牌管理",
                        path: "/product/trademark",
                        element: Lazyload("product/trademark"),
                    },
                    // 平台属性
                    {
                        label: "平台属性",
                        path: "/product/attr",
                        element: Lazyload("product/attr"),
                    },
                ],
            },
        ],
    },
    {
        // 登录页面
        path: "/login",
        element: Lazyload("Login"),
    },
    {
        // 404 NotFound
        path: "*",
        element: Lazyload("NotFound"),
    },
];

// 侧边栏和路由关联
export function renderMenuItems() {
    return (routes.find((value) => value.path === "/") as any).children;
}

// 注册所有路由
export const useAppRoutes = () => {
    return useRoutes(routes);
};
